<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * 获取用户信息
     */
    public function getUserInfo(Request $request)
    {
        $user = $request->user();
        
        return response()->json([
            'data' => [
                'id' => (string)$user->id,
                'nickname' => $user->name,
                'avatar' => $user->avatar ?? '',
                'roles' => ['admin'], // 暂时固定为admin，实际项目中需要根据角色表查询
                'permissions' => [
                    'user:view',
                    'user:edit', 
                    'user:delete',
                    'admin:all'
                ] // 暂时固定权限，实际项目中需要根据权限表查询
            ],
            'message' => '获取成功'
        ]);
    }

    /**
     * 获取用户路由
     */
    public function getUserRoutes(Request $request)
    {
        $user = $request->user();
        
        // 根据用户权限返回可访问的路由
        $routes = $this->buildUserRoutes($user);
        
        return response()->json([
            'data' => $routes,
            'message' => '获取成功'
        ]);
    }

    /**
     * 构建用户路由数据
     */
    private function buildUserRoutes($user)
    {
        // 示例路由数据，实际项目中应该根据用户角色和权限动态生成
        return [
            [
                'id' => '1',
                'title' => '首页',
                'name' => 'Dashboard',
                'path' => '/dashboard',
                'component' => 'Dashboard',
                'icon' => 'dashboard',
                'children' => []
            ],
            [
                'id' => '2',
                'title' => '用户管理',
                'name' => 'UserManagement',
                'path' => '/user',
                'component' => 'UserManagement',
                'icon' => 'user',
                'children' => [
                    [
                        'id' => '2-1',
                        'title' => '用户列表',
                        'name' => 'UserList',
                        'path' => '/user/list',
                        'component' => 'UserList',
                        'icon' => 'list'
                    ]
                ]
            ]
        ];
    }
}
